<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class Passport extends CI_Controller {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 跳转到登陆页面
	 */
	public function index() {
		/*
		$wcode = $this->input->get ( 'code' );//微信登陆
		if(!empty($wcode)){
			$appid="wx0f0f0044419cc032";
			$secret="c8140231c9727867349c582842178082";
			$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$wcode."&grant_type=authorization_code";
			$ch = curl_init ();
			curl_setopt ( $ch, CURLOPT_URL, $url );
			curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
			curl_setopt ( $ch, CURLOPT_HEADER, 0 );
			$output = curl_exec ( $ch );
			curl_close ( $ch );
			$output = json_decode($output);
			if(isset($output->openid)){
				$openid=$output->openid;
				$this->session->set_userdata ( "wopenid",$openid );
				$this->load->model ( 'oauth_model' );
				$oauth = $this->oauth_model->find_by_openidandtype ( $openid,2);
				if (! empty 
				( $oauth )) {
					$this->load->model ( 'user_model' );
					$user = $this->user_model->load ($oauth ['userid']);
					$data = array (
						'id' => $oauth ['userid'],
						'logged_in' => TRUE 
					);
					$this->session->set_userdata ( $data );
					$this->record($oauth ['userid'],2);
					redirect ( 'home/index' );
				}
			}
		}
		 */
		$this->load->view ( 'passport/login' );
	}
	/*
	 * 登陆验证
	 */
	public function login_v2() {
		$sno = $this->input->post ( 'sno' );
		$password = $this->input->post ( 'password' );
		
		$this->load->model ( 'user_model' );
		$user = $this->user_model->find_by_sno_password ( $sno, md5 ( $password . 'jishibao' ) );
		if (!empty($user)) {
			$data = array (
				'id' => $user ['id'],
				'logged_in' => TRUE 
			);
			$this->session->set_userdata ( $data );
			
			$this->record($user ['id'],0);			
			if($password==substr($sno,-6)){//密码为学号后6位则跳到修改密码的页面
				$datas['redirect']=TRUE;
			}else{
				$datas['redirect']=FALSE;
			}
			return_json ( TRUE, $datas, '登陆成功！' );
		} else {
			return_json ( FALSE, '', '学号或密码错误！' );
		}
	}
	/*
	 * 跳转到修改密码页面
	 */
	public function resetpassword() {
		$this->load->view ( 'passport/reset' );
	}
	/*
	 * 注销账户
	 */
	public function logout() {
		$data = array (
			'id' => '',
			'type' =>'',
			'logged_in' => '' 
		);
		$this->session->unset_userdata($data);//这是为了保留第三方登陆ID的seesion
		//$this->session->sess_destroy ();
		redirect ( 'passport/index' );
	}
	/*
	 * 记录登陆信息
	 */
	private function record($userid,$type) {
		$record = array (
			'userid' => $userid,
			'login_time' => date ( "Y-m-d H:i:s" ),
			'type'=>$type
		);
		$this->load->model ( 'record_model' );
		$this->record_model->create ($record);
	}
	
	
	/*
	 * 登陆验证
	 */
	public function login() {
		$username = $this->input->post ( 'username' );
		$password = $this->input->post ( 'password' );
		
		$this->load->model ( 'user_model' );
		$user = $this->user_model->get_user ( $username, md5 ( $password . 'jishibao' ) );
		if (!empty($user)) {
			$data = array (
					'id' => $user ['id'],
					'logged_in' => TRUE 
			);
			$this->session->set_userdata ( $data );
			/*
			$wopenid = $this->session->userdata ( 'wopenid' ); // 得到微信openid
			if (! empty ( $wopenid )) {
				$this->load->model ( 'oauth_model' );
				$oauth = $this->oauth_model->find_by_useridandtype ( $user ['id'],2 );
				if (empty ( $oauth )) {//如果该用户没有绑定过微信则绑定
					$newoauth = array (
							'userid' => $user ['id'],
							'openid' => $wopenid,
							'type' => 2,
							'regist_time' => date ( "Y-m-d H:i:s" )
					);
					$this->load->model ( 'oauth_model' );
					$oauthid = $this->oauth_model->create ( $newoauth );
					if (! empty ( $oauthid )) {
						$this->record($user ['id'],2);
						return_json ( TRUE, '', '登陆成功！' );
					}
				}else{
					$this->record($user ['id'],2);
					return_json ( TRUE, '', '登陆成功！' );
				}
			}
			*/
			$this->record($user ['id'],0);			
			return_json ( TRUE, '', '登陆成功！' );
		} else {
			return_json ( FALSE, '', '用户名或密码错误！' );
		}
	}
	/*
	 * 跳转到注册页面
	 */
	public function register() {
		/*
		$wcode = $this->input->get ( 'code' );//微信登陆
		if(!empty($wcode)){
			$appid="wx5491f669f2ced50f";
			$secret="bda20216cfcdd2ce33f14f0013aa8e42";
			$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$wcode."&grant_type=authorization_code";
			$ch = curl_init ();
			curl_setopt ( $ch, CURLOPT_URL, $url );
			curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
			curl_setopt ( $ch, CURLOPT_HEADER, 0 );
			$output = curl_exec ( $ch );
			curl_close ( $ch );
			$output = json_decode($output);
			if(isset($output->openid)){
				$openid=$output->openid;
				$this->session->set_userdata ( "wopenid",$openid );
				$this->load->model ( 'oauth_model' );
				$oauth = $this->oauth_model->find_by_openidandtype ( $openid,2);
				if (! empty ( $oauth )) {
					$this->load->model ( 'user_model' );
					$user = $this->user_model->load ($oauth ['userid']);
					$data = array (
							'id' => $oauth ['userid'],
							'logged_in' => TRUE 
					);
					$this->session->set_userdata ( $data );
					$this->record($oauth ['userid'],2);
					redirect ( 'home/index' );
				}
			}
		}*/
		$this->load->view ( 'passport/register' );
	}
	/*
	 * 获得学校信息
	 */
	public function getschools() {
		$this->load->model ( 'school_model' );
		$data = $this->school_model->get_schools ();
		if (! empty ( $data )) {
			return_json ( TRUE, $data, '查询成功！' );
		}
		return_json ( FALSE, '', '查询失败！' );
	}
	/*
	 * 获得年级信息(修改个人信息用到)
	 */
	public function getgrades() {
		$type = $this->input->post ( 'type' );
		$this->load->model ( 'school_model' );
		$data = $this->school_model->find_grade_by_type ($type);
		if (! empty ( $data )) {
			return_json ( TRUE, $data, '查询成功！' );
		}
		return_json ( FALSE, '', '查询失败！' );
	}
	/*
	 * 获得班级信息(修改个人信息用到)
	 */
	public function getclasses() {
		$school = $this->input->post ( 'school' );
		$grade = $this->input->post ( 'grade' );
		$this->load->model ( 'school_model' );
		$data = $this->school_model->find_class_by_school_grade ($school,$grade);
		if (! empty ( $data )) {
			return_json ( TRUE, $data, '查询成功！' );
		}
		return_json ( FALSE, '', '查询失败！' );
	}
	/*
	 * 发送注册的验证码给用户
	 */
	public function sendCode(){
		$phone = $this->input->post ( 'phone' );
		$this->load->model ( 'user_model' );
		$user = $this->user_model->find_by_username ( $phone );
		if (! empty ( $user ['id'] )) {
			return_json ( FALSE, '', '该手机已注册！' );
		}
		$sid="19fd07bb099b7243eb704446bc64f297";
		$appid="4ef49fb5eb794983aaa9b950898b7313";
		$key="4ead38201201410e2e6edace02d14e9f";
		$time=date("YmdHis").'111';
		$sign=md5($sid.$time.$key);
		$templateid="23034";
		$param=$this->build_code_no();//
		$this->session->set_userdata ( "code",$param );
		
		$url="http://www.ucpaas.com/maap/sms/code?sid=".$sid."&appId=".$appid."&time=".$time."&sign=".$sign."&to=".$phone."&templateId=".$templateid."&param=".$param;
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		$output = curl_exec ( $ch );
		curl_close ( $ch );
		$output = json_decode($output);
		if($output->resp->respCode=="000000"){//短信发送成功
			$message = array (
					'phone' => $phone,
					'state' => 1,
					'smsid' => $output->resp->templateSMS->smsId,
					'createdate' => $output->resp->templateSMS->createDate
			);
			$this->load->model ( 'message_model' );
			$this->message_model->create($message);//更新短信表
			return_json ( TRUE, "", '验证码发送成功！' );
		}
		return_json ( FALSE, "", '验证码发送失败！' );
	}
	/*
	 * 发送找回密码的验证码给用户
	 */
	public function sendResetCode(){
		$phone = $this->input->post ( 'phone' );
		$this->load->model ( 'user_model' );
		$user = $this->user_model->find_by_username ( $phone );
		if (empty ($user)) {
			return_json ( FALSE, '', '该手机未注册！' );
		}
		$sid="19fd07bb099b7243eb704446bc64f297";
		$appid="4ef49fb5eb794983aaa9b950898b7313";
		$key="4ead38201201410e2e6edace02d14e9f";
		$time=date("YmdHis").'111';
		$sign=md5($sid.$time.$key);
		$templateid="23477";
		$param=$this->build_code_no();//
		$this->session->set_userdata ( "code",$param );
		
		$url="http://www.ucpaas.com/maap/sms/code?sid=".$sid."&appId=".$appid."&time=".$time."&sign=".$sign."&to=".$phone."&templateId=".$templateid."&param=".$param;
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		$output = curl_exec ( $ch );
		curl_close ( $ch );
		$output = json_decode($output);
		if($output->resp->respCode=="000000"){//短信发送成功
			$message = array (
					'phone' => $phone,
					'state' => 1,
					'smsid' => $output->resp->templateSMS->smsId,
					'createdate' => $output->resp->templateSMS->createDate
			);
			$this->load->model ( 'message_model' );
			$this->message_model->create($message);//更新短信表
			return_json ( TRUE, "", '验证码发送成功！' );
		}
		return_json ( FALSE, "", '验证码发送失败！' );
	}
	/*
	 * 保存用户注册信息
	 */
	public function save() {
		$code = $this->input->post ( 'code' );
		$scode = $this->session->userdata ( 'code' );
		if($code!=$scode){
			return_json ( FALSE, '', '验证码错误!' );
		}
		$username = $this->input->post ( 'username' );
		$this->load->model ( 'user_model' );
		$user = $this->user_model->find_by_username ( $username );
		if(! empty ( $user ['id'] )){//防止重复注册
			return_json ( FALSE, '', '该手机号已注册！' );
		}
		$sname = $this->input->post ( 'sname' );
		$sno = $this->input->post ( 'sno' );
		$class=$this->input->post ( 'class' );
		$user = $this->user_model->find_by_class_name ( $class,$sname);
		if(! empty ( $user ['id'] )){
			$newuser = array (
				'username' => $username,
				'password' => md5 ( $this->input->post ( 'password' ) . 'jishibao' ),
				'phone' => $username,
				'pname' => $this->input->post ( 'pname' ),
				'regist_time' => date ( "Y-m-d H:i:s" ) 
			);
			if($this->user_model->update($user ['id'],$newuser)){
				$data = array (
					'id' => $user ['id'],
					'logged_in' => TRUE 
				);
				$this->session->set_userdata ( $data );
				$this->record($user ['id'],0);
				return_json ( TRUE, '', '注册成功！' );
			}else{
				return_json ( FALSE, '', '注册失败！' );
			}
		}else{
		$newuser = array (
				'username' => $username,
				'password' => md5 ( $this->input->post ( 'password' ) . 'jishibao' ),
				'phone' => $username,
				'pname' => $this->input->post ( 'pname' ),
				'sname' => $sname,
				'class' => $class,
				'sno' => $sno,
				'regist_time' => date ( "Y-m-d H:i:s" ) 
		);
		$userid = $this->user_model->create ( $newuser );
		if (! empty ( $userid )) { // 注册成功跳转到主页面
			$data = array (
					'id' => $userid,
					'logged_in' => TRUE 
			);
			$this->session->set_userdata ( $data );
			/*
			$wopenid = $this->session->userdata ( 'wopenid' ); // 得到微信openid
			if (! empty ( $wopenid )) {
				$oauth = array (
						'userid' => $userid,
						'openid' => $wopenid,
						'type' => 2,
						'regist_time' => date ( "Y-m-d H:i:s" ) 
				);
				$this->load->model ( 'oauth_model' );
				$oauthid = $this->oauth_model->create ( $oauth );
				if (! empty ( $oauthid )) {
					$this->record($userid,2);
					return_json ( TRUE, '', '注册成功！' );
				}
			}
			*/
			$this->record($userid,0);
			return_json ( TRUE, '', '注册成功！' );
		}else{
			return_json ( FALSE, '', '注册失败！' );
		}
		}
	}
	/*
	 * 更新用户密码
	 */
	public function updatepassword() {
		$name = $this->input->post ( 'phone' );
		$password = md5 ( $this->input->post ( 'password' ) . 'jishibao' );
		$code = $this->input->post ( 'code' );
		$scode = $this->session->userdata ( 'code' );
		if($code!=$scode){
			return_json ( FALSE, '', '验证码错误！' );
		}
		$this->load->model ( 'user_model' );
		$user=$this->user_model->find_by_username ( $name );
		$newuser = array (
				'password' => $password,
		);
		if ($this->user_model->update ( $user['id'], $newuser )) {
			$this->session->unset_userdata('code');
			return_json(TRUE,'','修改密码成功!');
		}else{
			return_json(FALSE,'','密码没有变化!');
		}
	}
	/*
	 * 生成6位短信验证码
	 */
	private function build_code_no() {
		$a = range ( 0, 9 );
		for($i = 0; $i < 6; $i ++) {
			$b [] = array_rand ( $a );
		}
		return implode("",$b);
	}
}